﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RsaRegister.Model
{
    public class RegInfo
    {
        [SugarColumn(IsPrimaryKey = true, IsIdentity = false,Length =36)]
        public Guid Id { get; set; }
        [SugarColumn(Length =36)]
        public Guid SoftId { get; set; }
        [SugarColumn(Length = 50)]
        public string SoftName { get; set; }
        [SugarColumn(Length = 50)]
        public string User { get; set; }
        public DateTime ExpireDate { get; set; }
        public int TotalUsers { get; set; }
        [SugarColumn(Length = 100)]
        public string Cpuid { get; set; }
        [SugarColumn(Length =36)]
        public Guid Guid { get; set; }
        public DateTime LastUpdated { get; set; }
        [SugarColumn(Length = 300)]
        public string Note { get; set; }
        [SugarColumn(Length = 50)]
        public string Contact { get; set; }
        [SugarColumn(Length = 50)]
        public string Telephone { get; set; }
        [SugarColumn(Length = 50)]
        public string Email { get; set; }
        public RegInfo()
        {
            Id = Guid.Empty;
            User = "000000";
            ExpireDate = new DateTime(2020, 12, 31);
        }
        public RegInfo(string regInfoStr)
        {
            string[] regStrs = regInfoStr.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var s in regStrs)
            {
                switch(s[0])
                {
                    case 'S':
                        SoftName = s.Substring(2);
                        break;
                    case 'U':
                        User = s.Substring(2);
                        break;
                    case 'E':
                        ExpireDate =Convert.ToDateTime( s.Substring(2));
                        break;
                    case 'T':
                        TotalUsers = Convert.ToInt32(s.Substring(2));
                        break;
                    case 'C':
                        Cpuid = s.Substring(2);
                        break;
                    case 'G':
                        Guid = Guid.Parse(s.Substring(2));
                        break;
                }
            }
           
        }

        public string GenerateRegString()
        {
            string gStr = "S="+SoftName + "|U=" + User;
            if (ExpireDate.Year != 1 && ExpireDate != DateTime.MinValue) gStr += "|E=" + ExpireDate.ToString("yyyy-MM-dd");
            if (TotalUsers > 0) gStr += "|T=" + TotalUsers.ToString();
            if (!string.IsNullOrEmpty(Cpuid)) gStr += "|C=" + Cpuid;
            if (Guid!=Guid.Empty) gStr += "|G=" + Guid;
            return gStr;
        }
       
    }
}
